<?php
namespace src\logic;
use \classlib\core as C;
class lnews {
	//获取未读消息记录数信息
	public static function get_unread_msg($userid, $dicteid=-1) {
		$user   = C\model::load('main.pl.userinfo')->set_dev($userid)->get_one($userid);
		$stime  = array($user['truncate'], $user['allread'], $user['regtime']);
		$stime  = max($stime);
		$where  = array('userid'=>$userid, array('status', 0, \classlib\orm::OP_GT),
				array('stime', $stime, \classlib\orm::OP_GT));
		$msgids = C\model::load('main.pl.mstatus')->set_dev($userid)->get_column(0, -1, $where, 'msgid');
		$where  = array('status'=>1, array('stime', $stime, \classlib\orm::OP_GT),
				array('reveiverid', array('0', $userid), \classlib\orm::OP_IN));
		//排除被删除和已经阅读的记录
		if (is_array($msgids) && !empty($msgids)) {
			$where[] = array('id', $msgids, \classlib\orm::OP_NOTIN);
		}
		if ($dicteid != -1) {
			$where['dicteid'] = $dicteid;
		}
		$total = C\model::load('main.pl.msg')->get_total($where);
		return $total;
	}
	
	//获取站内信信息列表
	public static function get_list($userid, $query, $dicteid, $start=0, $limit=10) {
		static $rtime = 0, $rmsgids = array(), $filter = array();
		$msgs    = array('total'=>0, 'nums'=>0, 'list'=>array());
		$sorm    = C\model::load('main.pl.mstatus');
		$morm    = C\model::load('main.pl.msg');
		$urom    = C\model::load('main.pl.userinfo');
		if (empty($filter)) {//通过静态变量控制不要二次执行相同的代码逻辑
			$user    = $urom->set_dev($userid)->get_one($userid);
			$dtime   = $stime = $rtime = $user['regtime'];
			$rtime   = ($user['allread'] > $user['regtime'])? $user['allread']:$user['regtime'];
			$dtime   = ($user['truncate'] > $user['regtime'])? $user['truncate']:$user['regtime'];
			$stime   = ($rtime > $dtime)? $rtime:$dtime;
			//获取已经被删除的msgids
			$where   = array('userid'=>$userid, 'status'=>2, array('stime', $dtime, \classlib\orm::OP_GT));
			$dmsgids = $sorm->set_dev($userid)->get_column(0, -1, $where, 'msgid');
			//获取已经被读取的msgids
			$where   = array('userid'=>$userid, 'status'=>1, array('stime', $rtime, \classlib\orm::OP_GT));
			$rmsgids = $sorm->set_dev($userid)->get_column(0, -1, $where, 'msgid');
			//合并两个数组资料信息
			$msgids  = (empty($dmsgids)? $rmsgids:(empty($rmsgids)? $dmsgids:array_merge($dmsgids, $rmsgids)));
			$filter  = self::__where($dicteid, $userid, $dmsgids, $dtime, $query);
		}
		$msgs['total'] = self::get_unread_msg($userid, $dicteid);
		if ($limit != 4) {//首页默认返回四条记录信息 不需要这个总数
			$msgs['nums']  = (int)$morm->get_total($filter);
		}
		//获取消息资料信息列表
		$field   = 'id,title,senderid,reftype,refid as userid,stime';
		$list    = $morm->get_list($start, $limit, $filter, $field, 'id', \classlib\orm::DESC);
		if ($list) {
			foreach ($list as &$item) {
				$item['status'] = (($item['stime'] < $rtime) || ($rmsgids && in_array($item['id'], $rmsgids)))? 1:0;
				if ($item['userid'] > 0 && $item['userid'] != $userid) {
					$user = $urom->set_dev($item['userid'])->get_one($item['userid']);
					$item['nickname'] = $user['nickname'];
					$item['avtar']    = $user['thumb'];
				}
			}
		}
		$msgs['list']  = $list;
		return $msgs;
	}
	
	//获取条件限制的资源整合
	private static function __where($dicteid, $userid, $dmesgids, $dtime, $query) {
		$where = array(
			array('dicteid', $dicteid, \classlib\orm::OP_EQ), 
			array('status', 1, \classlib\orm::OP_EQ),
			array('stime', $dtime, \classlib\orm::OP_GT), 
			array('reveiverid', array('0', $userid), \classlib\orm::OP_IN)
		);
		if (is_array($dmesgids) && !empty($dmesgids)) { //排除被删除的记录
			$where[] = array('id', $dmesgids, \classlib\orm::OP_NOTIN);
		}
		if ($query) {
			$query = '%'.$query.'%';
			$where[] = array('(');
			$where[] = array('msg', $query, \classlib\orm::OP_LIKE);
			$where[] = array('title', $query, \classlib\orm::OP_LIKE, \classlib\orm::OP_OR);
			$where[] = array(')');
		}
		return $where;
	}
	
	public static function send($title, $msg, $receiveid, $reftype=0, $refid=0, $dictid=1, $senderid=0, $lng='en') {
		$data = array('senderid'=>$senderid, 'dicteid'=>$dictid, 'title'=>$title, 'reftype'=>$reftype, 'lng'=>$lng,
				'refid'=>$refid, 'msg'=>$msg, 'reveiverid'=>$receiveid, 'status'=>1, 'stime'=>C\config::$nowtime);
		C\model::load('main.pl.msg')->new_one($data);
	}
	
	public static function onesignal($receiveid, $title, $msg, $msgtype=0, $studioid=0, $attachs=null) {
		$one = C\model::load('main.log.onesignal')->get_one($receiveid);
		if (!$one) {
			return;
		}
		$conf = C\config::load('onesignal', false);
		$data = array('messageType'=>strval($msgtype), 'studioID'=>strval($studioid), 'userID'=>$receiveid);
		$args = array('app_id'=>$conf['appid'], 'contents'=>array('en'=>$msg), 'data'=>$data,
				'headings'=>array('en'=>$title), 'include_player_ids'=>array($one['player']));
		if ($attachs && is_array($attachs)) {
			$args = array_merge($args, $attachs);
		}
		$args = json_encode($args);
		$header = array('Content-Type: application/json', 'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj');
		\classlib\request::curl($conf['notify'], $args, $header, 'POST');
	}
}